from flask import Blueprint, make_response, render_template, request, session, redirect
from flask import url_for
from flask_restful import Api, Resource
from app.route import routes
from app.services.service_user import service_user

web_api = Blueprint('web_api', __name__, template_folder='templates')

# @web_api.before_request
# def author():
# 	token = request.args.get('token')
# 	service_user.service_user.match_token(token)


@web_api.route('/', endpoint='index')
def index():
	data = dict()
	token = session.get("token")
	if token is not None and service_user.match_token(token) is True:
		user_info = service_user.get_user_by_token(token)
		data["user"] = user_info
	return render_template('index.html', **data)


@web_api.route('/login', endpoint='login')
def login():
	token = session.get("token")
	if service_user.match_token(token) is True:
		return redirect(url_for('web_api.index'))
	return render_template('login/login.html')

@web_api.route('/register', endpoint='register')
def register():
	token = session.get("token")
	if service_user.match_token(token) is True:
		return redirect(url_for('web_api.index'))
	return render_template('login/register.html')
